/*
    公共——号牌号码
publicPlateNo.initIsHelpShow(true);//true|显示车辆号码帮助信息
publicPlateNo.initIsHelpShow(false); //false|隐藏车辆号码帮助信息
    1.使用方法：
    获取值的方式：
    判断车辆号码是否合理（已经包含了不合理时的提示）
    //inventoryObj.exportParam.plateNo 是保有量页面的变量，使用组件请更换成自己的变量
if (!publicPlateNo.getPlateNo()) {//号牌号码非必填，不传参数
    return;
} else {
    inventoryObj.exportParam.plateNo = publicPlateNo.getValue();
}

 if (!publicPlateNo.getPlateNo(true)) {//号牌号码必填，传参数true (注意)
    return;
} else {
    endpointObj.plateNo = publicPlateNo.getValue();
}

   2.引入js文件
   <script src="/vias_app/static/vias/js/common/publicPlatenoMethod.js?v=2.100"></script>
   <div class="plateno"></div>
 */

var publicPlateNo = function () {
    var moduleId ="";//组件编号
    var obj = {};
    var object = {};
    var objectValue = "";
    var widthInput = "";
    var helpMinInfoMargin = "";
    return {
        init: function () {
            obj = $('.plateno' + moduleId);
            this.initWidthInput();
            this.initPlateNo(obj);
            this.initPlateNoEvent(obj);
            this.initDropdownPlace();
            this.initHelp();
            this.initStopPropagation();
        },
        /**
         * 使用方法-
         * publicPlateNo.initIsHelpShow(true);
         * publicPlateNo.initIsHelpShow(false);
         * 以上不在本JS初始化，在需要的页面初始化即可
         * @param showHelpInfo  true|显示  false|隐藏
         */
        initIsHelpShow:function(showHelpInfo){
            if (showHelpInfo == false) {
                $('.plateno' + moduleId + ' .plate-i').attr("class", "");
                $('.plateno' + moduleId + ' .plate-i').remove();
            }
        },
        /**
         *  初始号牌号码长度  添加widthPlate 作为class类代表 width:270px;
         */
        initWidthInput:function () {
            var classWidth = $('.plateno' + moduleId).attr("class").split(" ");
            var flag = 0;
            $.each(classWidth, function (i, d) {
                if (d == "widthPlate") {
                    widthInput = '360px';
                    flag = 1;
                    return;
                }
                if (d == "middlePlate") {
                    helpMinInfoMargin = "250px";
                    widthInput = '200px';
                    flag = 2;
                    return;
                }
            });
            if (flag == 0) {
                helpMinInfoMargin = "180px";
                widthInput = '122px';
            }
        },
        /**
         *  初始化阻止冒泡
         */
        initStopPropagation: function () {
            //阻止高级选项区域点击收起面板
            $('.panel-body').click(function (e) {
                e.stopPropagation();
            });
        },
        /**
         * 初始化是否显示帮助提示
         */
        initHelp: function () {
            $(".plate-ul .plateno-panel > a > span").click(function () {
                var letterArr = $(".cp .plate-ul .plateno-panel");
                $.each(letterArr, function (i, d) {
                    $.each(d, function (n, m) {
                        if(n =="id"){
                            $("#" + m + " > a > span").attr("class","badge badge-primary");
                        }
                    });
                });
                $(this).attr("class","badge badge-primary plate-btn");
            });
        },
        /**
         * 初始化下拉框的位置
         */
        initDropdownPlace: function () {
            var plateNoOffsetLeft = $('.plateno' + moduleId).prev().offset().left; //下拉框距离窗口左侧宽度
            var plateNoWidth = $('.plateno' + moduleId).next().width(); //下拉框的宽度
            var windowWidth = $(window).width(); //窗口的宽度
            if (windowWidth - plateNoOffsetLeft > plateNoWidth) {
                $('.plateno' + moduleId).next().addClass('dropdown-menu-left');
                $('.plateno' + moduleId).next().css("left", "5px");
            } else {
                $('.plateno' + moduleId).next().addClass('dropdown-menu-right');
                $('.plateno' + moduleId).next().css("right", "5px");
            }
        },
        /**
         * 初始化号牌号码组件
         */
        initPlateNo: function (o) {
            if (!o) {
                o = obj;
            }
            var html = ' <div class="layui-form-item input-group no-padding cp" style="margin-bottom: -5px;">' ;
            html += '    <input autocomplete="off" type="hidden" id="plateno_input"/>';
            html += '    <input  autocomplete="off" type="hidden" id="plateno_prefix"/>';
            html += '    <label class="dropdown-toggle layui-form-label " data-toggle="dropdown" aria-expanded="" ' +
                'style="width: 80px;height: 100%;padding-left: 15px;padding-right: 2px;border-radius: 2px 0 0 2px;border-style: solid;">';
            html += '<span  id="plateno_btn">全部</span>';
            html += '<span class="caret" style="margin-left: 8px;margin-right: 8px;"></span></label>';
            html += '    <ul class="dropdown-menu pull-left plate-ul" role="menu" style="height: 375px;margin-top: -2px;">';
            html += '        <div class="panel-body layui-row plateno-panel" id="plateno-div">';
            html += '            <a class="margin-div" href="#" prefix="京"><span class="badge badge-primary">京</span></a>';
            html += '            <a class="margin-div" href="#" prefix="浙"><span class="badge badge-primary">浙</span></a>';
            html += '            <a class="margin-div" href="#" prefix="苏"><span class="badge badge-primary">苏</span></a>';
            html += '            <a class="margin-div" href="#" prefix="吉"><span class="badge badge-primary">吉</span></a>';
            html += '            <a class="margin-div" href="#" prefix="沪"><span class="badge badge-primary">沪</span></a>';
            html += '            <a class="margin-div" href="#" prefix="津"><span class="badge badge-primary">津</span></a>';
            html += '            <a class="margin-div" href="#" prefix="渝"><span class="badge badge-primary">渝</span></a>';
            html += '            <a class="margin-div" href="#" prefix="蒙"><span class="badge badge-primary">蒙</span></a>';
            html += '            <a class="margin-div" href="#" prefix="宁"><span class="badge badge-primary">宁</span></a>';
            html += '            <a class="margin-div" href="#" prefix="桂"><span class="badge badge-primary">桂</span></a>';
            html += '            <a class="margin-div" href="#" prefix="黑"><span class="badge badge-primary">黑</span></a>';
            html += '            <a class="margin-div" href="#" prefix="辽"><span class="badge badge-primary">辽</span></a>';
            html += '            <a class="margin-div" href="#" prefix="冀"><span class="badge badge-primary">冀</span></a>';
            html += '            <a class="margin-div" href="#" prefix="晋"><span class="badge badge-primary">晋</span></a>';
            html += '            <a class="margin-div" href="#" prefix="鲁"><span class="badge badge-primary">鲁</span></a>';
            html += '            <a class="margin-div" href="#" prefix="豫"><span class="badge badge-primary">豫</span></a>';
            html += '            <a class="margin-div" href="#" prefix="皖"><span class="badge badge-primary">皖</span></a>';
            html += '            <a class="margin-div" href="#" prefix="闽"><span class="badge badge-primary">闽</span></a>';
            html += '            <a class="margin-div" href="#" prefix="赣"><span class="badge badge-primary">赣</span></a>';
            html += '            <a class="margin-div" href="#" prefix="湘"><span class="badge badge-primary">湘</span></a>';
            html += '            <a class="margin-div" href="#" prefix="鄂"><span class="badge badge-primary">鄂</span></a>';
            html += '            <a class="margin-div" href="#" prefix="粤"><span class="badge badge-primary">粤</span></a>';
            html += '            <a class="margin-div" href="#" prefix="琼"><span class="badge badge-primary">琼</span></a>';
            html += '            <a class="margin-div" href="#" prefix="陕"><span class="badge badge-primary">陕</span></a>';
            html += '            <a class="margin-div" href="#" prefix="川"><span class="badge badge-primary">川</span></a>';
            html += '            <a class="margin-div" href="#" prefix="贵"><span class="badge badge-primary">贵</span></a>';
            html += '            <a class="margin-div" href="#" prefix="云"><span class="badge badge-primary">云</span></a>';
            html += '            <a class="margin-div" href="#" prefix="新"><span class="badge badge-primary">新</span></a>';
            html += '            <a class="margin-div" href="#" prefix="甘"><span class="badge badge-primary">甘</span></a>';
            html += '            <a class="margin-div" href="#" prefix="青"><span class="badge badge-primary">青</span></a>';
            html += '            <a class="margin-div" href="#" prefix="藏"><span class="badge badge-primary">藏</span></a>';
            html += '            <a class="margin-div" href="#" prefix="台"><span class="badge badge-primary">台</span></a>';
            html += '            <a class="margin-div" href="#" prefix="港"><span class="badge badge-primary">港</span></a>';
            html += '            <a class="margin-div" href="#" prefix="澳"><span class="badge badge-primary">澳</span></a>';
            html += '            <div style="text-align: center;padding-top: 5px;"><button id="confirm" class="" style="margin: 0px 10px; width: 60px; height: 30px; background-color: #1890ff; border-radius: 6px; border: none" onclick="publicPlateNo.confirmButton()">确认</button><button id="reset" class="" style="margin: 0px 10px; width: 60px; height: 30px; border-radius: 6px; border: none" onclick="publicPlateNo.resetButton()">重置</button></div> ';
            html += '        </div>';
            html += '    </ul>';
            html += '    <input placeholder="号牌号码" autocomplete="off" class="layui-input-inline layui-input" type="text" id="plateno_suffix" name="plateno_suffix" style="width: '+widthInput+'">';
            html += '</div>';
            html += '<i class="icon-icon-121-info1 dropdown-toggle plate-i helpInfoPic" data-toggle="dropdown" style="cursor: pointer; position: absolute;color: #cbd1d5;top: 0;left: 0;margin-top: 12px;margin-left: '+helpMinInfoMargin+'"></i>';
            html += '<div class="dropdown-menu animated fadeIn" style="font-size: 14px;color: #666666;">';
            html += '   <div class="dropdown-body" style="width: 770px">';
            html += '       <div class="panel panel-default ">';
            html += '           <div class="panel-body">';
            html += '               <div class="row">';
            html += '               <dl class="public_plate_no_div_dl">';
            html += '               <dt><p>1.号牌号码精确查询</p></dt>';
            html += "               <dd><p>(1)选择'沪'，输入精确的号牌号码:<span color='red'>A88888</span>，检索出号牌号码为'沪A88888'；</p></dd>";
            html += '<dt><p>2.号牌号码模糊查询</dt>';
            html += "<dd><p>(1)选择'沪'，输入模糊的号牌号码:<font color='red'>%A888</font>，检索出车辆省份为'沪'，以'A888'结尾的号牌号码；</p></dd>";
            html += "<dd><p>(2)选择'沪'，输入模糊的号牌号码:<font color='red'>%A%888</font>，检索出车辆省份为'沪'，中间包含'A'，并且结尾为'888'的号牌号码；";
            html += '</p></dd>';
            html += "<dd><p>(3)选择'全部'，输入模糊的号牌号码:<font color='red'>%A8%5%8</font>，检索出以'A8'开头，中间包含'5'，并且结尾为'8'的号牌号码；";
            html += '</p></dd>';
            html += "<dd><p>(4)选择'沪'，输入模糊的号牌号码:<font color='red'>A8%58%</font>，检索出车辆省份为'沪'，以'A8'开头，中间包含'58'的号牌号码。";
            html += '</p></dd>';
            html += '</dl>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            $(o).html(html);
            publicPlateNo.initSelectPlateNo(o);
        },
        initSelectPlateNo: function (o) {
            if (!o) {
                o = obj;
            }
            publicPlateNo.setPlateNoValue("", o);
        },
        /**
         * 初始化号牌号码事件
         */
        initPlateNoEvent: function (o) {
            if (!o) {
                o = obj;
            }
            $(o).find("a").on("click", function () {
                var value = $(this).attr("prefix");
                object = o;
                objectValue = value;//沪，苏，吉
                //publicPlateNo.confirmButton(value, o);
                publicPlateNo.selectPlateNo(value, o);
            });
        },
        selectPlateNo: function (value, o) {
            if (!o) {
                o = obj;
            }
            var oldprefix = $(o).find('#plateno_prefix').val();
            $(o).find('#plateno_btn').text(value);
            // $(o).find('#plateno_prefix').val(value);
            var plateNoValue = $(o).find('#plateno_suffix').val();
            // if (plateNoValue.startWith(oldprefix)) {//输入框中的内容包含上一次的省份
            if (true) {
                if (value == '全部') {
                    $(o).find('#plateno_suffix').val(plateNoValue.replace(oldprefix, ""));
                } else {
                    $(o).find('#plateno_suffix').val(plateNoValue.replace(oldprefix, value));
                    //输入框中是否有中文
                    if(isChinese(plateNoValue.substr(0,2))){
                        $(o).find('#plateno_suffix').val(value + plateNoValue.substr(1,plateNoValue.length-1));
                    }else{
                        $(o).find('#plateno_suffix').val(value + plateNoValue);
                        // $(o).find('#plateno_suffix').val(plateNoValue);
                    }
                }
            }
        },
        /**
         * 获得号牌号码的值
         * @param isNotNull  true|必填   其他|非必填
         * @param o
         * @returns {boolean}
         */
        getPlateNo: function (isNotNull, o) {
            if (!o) {
                o = obj;
            }
            var success = true;
            // 参数处理
            var provinceName = $(o).find("#plateno_prefix").val();//省份
            var plateno = $(o).find("#plateno_suffix").val();//号牌号码
            /* if((provinceName && plateno.length == 0)
                 || (plateno && !provinceName )
                 || (provinceName == '全部' && plateno!='null' )){*/
            if(provinceName && plateno.length == 0){
                $.notification.information({
                    text: '<i class="icon-icon-122-info2" style="padding-right: 15px;color: #1890ff;"></i>请输入车辆号码',
                    timeout: 2000,
                    layout: 'center'
                });
                success = false;
                plateno = "";
            }
            //
            if(plateno){
                //模糊查询，不走正则校验
                var btnSF = $("#plateno_btn").text();
                if(!isLicenseNo(plateno,btnSF) && plateno.indexOf("%")==-1){
                    $.notification.information({
                        text: '<i class="icon-icon-122-info2" style="padding-right: 15px;color: #1890ff;"></i>请输入正确的车辆号码',
                        timeout: 2000,
                        layout: 'center'
                    });
                    success = false;
                    plateno = "";
                }
                //格式正确
                if (plateno.length > 10) {
                    $.notification.information({
                        text: '<i class="icon-icon-122-info2" style="padding-right: 15px;color: #1890ff;"></i>输入的号牌号码不能大于10个字符',
                        timeout: 2000,
                        layout: 'center'
                    });
                    success = false;
                    plateno = "";
                }
            }else  if (isNotNull) {
                $.notification.information({
                    text: '<i class="icon-icon-122-info2" style="padding-right: 15px;color: #1890ff;"></i>请输入车辆号码',
                    timeout: 2000,
                    layout: 'center'
                });
                success = false;
                plateno = "";
            }
            $(o).find("#plateno_input").val(plateno);
            $(o).find("#plateno_prefix").val(provinceName);
            return success;
        },
        getPlateNoComplete: function (o) {
            var plateNoValue  = $("#plateno_suffix").val();
            var plateno  = "";
            if(!isChinese(plateNoValue.substr(0,2)) && $("#plateno_btn").text() != '全部' && plateNoValue && plateNoValue.substr(0,2)!="WJ" && plateNoValue.substr(0,2) != "wj") {
                plateno = $("#plateno_btn").text()+$("#plateno_input").val();
            }else {
                plateno = $("#plateno_input").val();
            }
            return plateno.toUpperCase();
        },
        setPlateNoValue: function (value, o) {
            if (!o) {
                o = obj;
            }
            if (value) {
                publicPlateNo.selectPlateNo(value, o);
                $(o).find("#plateno_suffix").val(value);
                $(o).find("#plateno_input").val(value);
            }
        },
        /**
         * 清除已填写的内容 不用废弃
         */
        clear: function () {
            $(obj).find("#plateno_input").val("");
        },
        /**
         * 获取号牌号码
         */
        getValue: function () {
            return publicPlateNo.getPlateNoComplete();
        },

        /**
         * 确认按钮
         */
        confirmButton: function () {
            $('.cp').removeClass("open");
            $('#plateno_btn').attr("aria-expanded","false");
            publicPlateNo.selectPlateNo(objectValue==""?"全部":objectValue, object);
        },
        /**
         * 重置按钮
         */
        resetButton: function () {
            objectValue = "";
            $("#plateno_btn").text("全部");
            $("#plateno_prefix").val("");/*不在此赋值  注意*/
            var letterArr = $(".cp .plate-ul .plateno-panel");
            $.each(letterArr, function (i, d) {
                $.each(d, function (n, m) {
                    if (n == "id") {
                        $("#" + m + " > a > span").attr("class", "badge badge-primary");
                    }
                });
            });
            $("#plateno_suffix").val("");
        }
    };
}();
publicPlateNo.init();

/**
 * 判断是否包含汉字
 * @param temp
 */
function isChinese(temp) {
    var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
    if(reg.test(temp)) return true;
    return false;
}

/**
 * 号牌号码的校验
 */
function isPlateNo(str) {
    var result = false;
    if (str.length == 7) {
        var express = /(^[\u4E00-\u9FA5]{1}[A-Za-z0-9]{6}$)|(^[A-Za-z]{2}[A-Za-z0-9]{2}[A-Za-z0-9\u4E00-\u9FA5]{1}[A-Za-z0-9]{4}$)|(^[\u4E00-\u9FA5]{1}[A-Za-z0-9]{5}[挂学警军港澳]{1}$)|(^[A-Za-z]{2}[0-9]{5}$)|(^(08|38){1}[A-Za-z0-9]{4}[A-Za-z0-9挂学警军港澳]{1}$)/;
        result = express.test(str);
    }
    if (str.length == 8) {
        var express = /(^[\u4E00-\u9FA5]{1}[A-Za-z0-9]{7}$)|(^[A-Za-z]{2}[A-Za-z0-9]{2}[A-Za-z0-9\u4E00-\u9FA5]{1}[A-Za-z0-9]{4}$)/;
        result = express.test(str);
    }
    return result;
}

/*校验号牌号码*/
function isLicenseNo(str,param) {
    if (param != '全部' && !isChinese(str.substr(0,2)) && str.substr(0,2)!="wj" && str.substr(0,2)!="WJ") {
        return isPlateNo(param+str);
    }else {
        if((str.substr(0,2)=="WJ")||(str.substr(0,2)=="wj")){
            if (str.length == 8) {
                return true;
            }
        }else {
            return isPlateNo(str);
        }
    }
}

